package com.y.report.hepler;

import java.lang.reflect.Field;
import java.sql.*;
import java.util.*;

public class ResultSetUtil {

    public static List<Map<String, Object>> getList(ResultSet rs) {
        List<Map<String, Object>> targets = new ArrayList<>();
        try {
            ResultSetMetaData rsmd = rs.getMetaData();
            int colCnt = rsmd.getColumnCount();
            String[] columnNames = new String[colCnt];
            Object[] columnValues = new Object[colCnt];
            Class<?>[] columnTypes = new Class<?>[colCnt];
            for (int i = 0; i < colCnt; i++) {
                columnNames[i] = rsmd.getColumnLabel(i + 1);
            }
            while (rs.next()) {
                for (int i = 0; i < colCnt; i++) {
                    String columnName = columnNames[i];
                    Object columnValue = rs.getObject(columnName);
                    Class<?> columnType = null;
                    if (columnValue != null) {
                        columnType = columnValue.getClass();
                    }
                    columnValues[i] = columnValue;
                    columnTypes[i] = columnType;
                }
                Map<String, Object> target = new LinkedHashMap<>();
                for (int i = 0; i < colCnt; i++) {
                    String columnName = columnNames[i];
                    Object columnValue = columnValues[i];
                    target.put(columnName, columnValue);
                }
                targets.add(target);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return targets;
    }

    public static <T> List<T> getList(ResultSet rs, Class<T> clazz) {
        List<Map<String, Object>> ls = getList(rs);
        List<T> targets = new ArrayList<>();
        try {
            for (int i = 0; i < ls.size(); i++) {
                Map<String, Object> x = ls.get(i);
                T target = clazz.newInstance();
                for (Iterator<Map.Entry<String, Object>> it = x.entrySet().iterator(); it.hasNext(); ) {
                    Map.Entry<String, Object> y = it.next();
                    String columnName = y.getKey();
                    Object columnValue = y.getValue();
                    Field field = clazz.getDeclaredField(columnName);
                    //Class<?> fieldType = field.getType();
                    field.setAccessible(true);
                    field.set(target, columnValue);
                }
                targets.add(target);
            }
        } catch (InstantiationException | IllegalAccessException | NoSuchFieldException e) {
            throw new RuntimeException(e);
        }
        return targets;
    }

    public static void testQ(String sss) {
        try {
            Class.forName("io.trino.jdbc.TrinoDriver");
            Connection conn = DriverManager.getConnection("jdbc:trino://10.192.112.69:18091", "zt17606", null);
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery("select now(),version()");
            List<Map<String, Object>> list = ResultSetUtil.getList(rs);
            rs.close();
            stmt.close();
            conn.close();
            System.out.println(sss + " 22222result:" + list);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}
